<template lang="html">
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="160px">
      <el-form-item label="服务分类等级">
        <el-select v-model="form.classify_type" placeholder="请选择">
          <el-option label="二级分类" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="服务分类标题">
        <el-col :span="6">
          <el-input v-model="form.classify_name" placeholder="请输入"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="所属一级服务分类">
        <el-select v-model="form.classify_branch_id" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.eq"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排 序">
        <el-col :span="6">
          <el-input type="number" v-model="form.eq"></el-input>
        </el-col>
        <el-col :span="6">
          &nbsp&nbsp&nbsp可填数字1-50，数字越靠前，代表排序靠前
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button class="buttonall" @click="onSubmit" type="primary">确认添加</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import request from '@/utils/request'
  export function createClass(mes,url) {
    return request({
      url: url,
      method: 'post',
      data: mes
    })
  }
  export default {
    data() {
      return {
        form: {
          classify_type: 2,
          classify_name: '',
          classify_branch_id: ''
        },
        options: []
      }
    },
    created: function(){
      const n = {
        page: 1,
        size: 9999,
        classify_type: 1
      }
      createClass(n,'/v1/classify').then(res => {
          if (res.code === 200) {
            if (res.data.list.length) {
              res.data.list.filter(item => {
                const val = {
                  value: item._id,
                  label: item.classify_name,
                }
                this.options.push(val)
              })
            }
          } else {
            this.$message({
              message: res.msg,
              type: 'error'
            });
          }
        }).catch(error => {
          this.$message({
            message: error,
            type: 'error'
          });
      })
    },
    methods: {
      onSubmit() {
        console.log(this.$data.form);
        createClass(this.$data.form,'/v1/classify/create').then(res => {
            if (res.code === 200) {
              this.$message({
                message: 'success!',
                type: 'success'
              })
              this.$router.go(-1)
            } else {
              this.$message({
                message: res.msg,
                type: 'error'
              });
            }
          }).catch(error => {
            this.$message({
              message: error,
              type: 'error'
          });
        })
      },
      handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
      }
    }
  }
</script>
<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
